package com.molichuxing.services.infrastructure.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.services.infrastructure.bean.PhoneRecognitionBean;
import com.molichuxing.services.infrastructure.dao.PhoneRecognitionMapper;
import com.molichuxing.services.infrastructure.dto.request.create.PhoneRecognitionCreateDto;
import com.molichuxing.services.infrastructure.dto.response.PhoneRecognitionDto;
import com.molichuxing.services.infrastructure.service.PhoneRecognitionService;
import com.molichuxing.services.property.VerifyResultEnum;

/**
 * 
 * @author liyg
 * 手机号验证服务实现类
 * 2019-08-15
 * 
 */
@Service("phoneRecognitionService")
public class PhoneRecognitionServiceImpl implements PhoneRecognitionService{

	@Autowired
	private PhoneRecognitionMapper phoneRecognitionMapper;
	
	
	/**
	 * 获取最近手机号验证是否通过
	 * @param userId
	 * @return
	 */
	@Override
	public PhoneRecognitionDto getVerifyResult(Integer userId) {
		
		PhoneRecognitionDto dto=null;
		
		List<PhoneRecognitionBean> list = phoneRecognitionMapper.getListByUserId(userId);
		if( null != list && list.size() > 0 ) {
			PhoneRecognitionBean bean = list.get(0);
			dto= new PhoneRecognitionDto();
			BeanUtils.copyProperties(bean, dto);
		}
		
		return dto;
	}
	
	/**
	 * 获取验证结果
	 * @param userId
	 * @param verifyTime
	 * @return
	 */
	@Override
	public Boolean getVerifyResult(Integer userId, LocalDateTime verifyTime) {
		
		if(null == userId) {
			return false;
		}
		List<PhoneRecognitionBean> list = phoneRecognitionMapper.getVerifyResult(userId,verifyTime);
		if( null == list || list.isEmpty() ) {
			return false;
		}
		for (PhoneRecognitionBean bean : list) {
			if(VerifyResultEnum.PASS.getValue() == bean.getVerifyResult()) {
				return true;
			}
		}	
		return false;
	}
	
	/**
	 * 保存
	 * @param createDto
	 * @return
	 */
	@Override
	public Boolean create(PhoneRecognitionCreateDto createDto) {
		PhoneRecognitionBean bean=new PhoneRecognitionBean();
		BeanUtils.copyProperties(createDto, bean);
		int num = phoneRecognitionMapper.create(bean);
		return num==1;
	}

}
